package com.doucome.snapshot.view
{
	import com.doucome.snapshot.Global;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	
	/**
	 * ...
	 * @author wkl
	 */
	public class PingFenView extends Sprite
	{
		
		private var _sList:Array;
		private var ping:Boolean = false;
		public static const PING_FEN_FINISH_EVENT:String = "PingFenFinishEvent";
		public var _fen:int = 0;
		public var _starView:Sprite;
		private var _photono:String = "";
		private var _photoid:int
		private var _photo_userid:int;
		private var _pfid:int;
		//private var _waitEffect:WaitEffect;
		private var _info_fen:TextField;
		
		public function PingFenView()
		{
			_sList = [];
			var s_w:int = 43; //star width
			var s_x:int = 56; //init star.x
			var s_y:int = 56; //txt height
			_starView = new Sprite()
			addChild(_starView);
			for (var i:int = 0; i < 5; i++)
			{
				var star:BtnStar = new BtnStar();
				star.index = i;
				star.x = s_x + (s_w * i);
				star.y = (s_y - s_w) / 2;
				star.addEventListener(MouseEvent.MOUSE_OVER, onOver);
				star.addEventListener(MouseEvent.MOUSE_OUT, onOut);
				star.addEventListener(MouseEvent.CLICK, onClick);
				star.stop();
				star.buttonMode = true;
				_starView.addChild(star);
				_sList.push(star);
			}
			
			//_waitEffect = new WaitEffect();
			//_waitEffect.x = s_x;
			//_waitEffect.y = (s_y - s_w) / 2;
			//_waitEffect.stop();
			//_waitEffect.visible = false;
			//addChild(_waitEffect);
			
			var stylesheet:StyleSheet = new StyleSheet();
			stylesheet.parseCSS(".info {font-size: 14px;font-family: Verdana, Arial, Helv, Helvetica, sans-serif;vertical-align: super;}");
			_info_fen = new TextField();
			_info_fen.styleSheet = stylesheet;
			_info_fen.autoSize = "left";
			_info_fen.selectable = false;
			_info_fen.x = 200;
			_info_fen.y = 52;
			_info_fen.htmlText = "";
			addChild(_info_fen);
		
			//trace(_sList.length);
		}
		
		private function onClick(e:MouseEvent):void
		{
			this.onOver(e);
			ping = true;
			var star:BtnStar = e.currentTarget as BtnStar;
			_fen = (star.index + 1) * 20;
			dispatchEvent(new Event(Event.OPEN));
			sendFen();
		
		}
		
		private function onOut(e:MouseEvent):void
		{
			
			if (!ping) // reset()
			{
				reset();
			}
		
		}
		
		private function onOver(e:MouseEvent):void
		{
			if (!ping)
			{
				reset();
				var star:BtnStar = e.currentTarget as BtnStar;
				var j:int = star.index;
				
				_info_fen.htmlText= "<p><span class='info'>"+(j+1)*20+"</span></p>";
				
				for (var i:int = 0; i <= j; i++)
				{
					var s:MovieClip = _sList[i];
					s.gotoAndStop(2);
				}
			}
		
		}
		
		public function turnOff():void
		{
			ping = false;
			this.visible = false;
			reset();
		
		}
		
		private function reset():void
		{
			for (var i:int = 0; i < _sList.length; i++)
			{
				var s:MovieClip = _sList[i];
				s.gotoAndStop(1);
			}
			//_waitEffect.visible = false;
			_info_fen.htmlText= "<p><span class='info'></span></p>";
		}
		
		public function turnON(photoid:int, photo_userid:int, pfid:int):void
		{
			turnOff();
			this._photoid = photoid;
			this._photo_userid = photo_userid;
			this._pfid = pfid;
			_starView.visible = true;
			this.visible = true;
		
		}
		
		public function get fen():int
		{
			return _fen;
		}
		
		public function set fen(value:int):void
		{
			_fen = value;
		}
		
		public function get photono():String
		{
			return _photono;
		}
		
		public function set photono(value:String):void
		{
			_photono = value;
		}
		
		private function sendFen():void
		{
			//trace("_photono" + _photono);
			//trace("_userid" + _userid);
			//trace("_fen" + _fen);
			var vars:URLVariables = new URLVariables("a=pingfen&photo_userid=" + _photo_userid + "&photoid=" + _photoid + "&fen=" + _fen + "&pfid=" + _pfid);
			var rq:URLRequest = new URLRequest();
			rq.url = "http://" + Global.APP_SERVER + "/snapshot/index.php";
			rq.method = URLRequestMethod.GET;
			rq.data = vars;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.OPEN, onSendFenOpen);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onSendFenError);
			loader.addEventListener(Event.COMPLETE, onSendFenComplete);
			loader.load(rq);
		
		}
		
		private function onSendFenError(e:IOErrorEvent):void
		{
			trace("unable to load url" + e);
			reset();
			dispatchEvent(new Event(IOErrorEvent.IO_ERROR));
		}
		
		private function onSendFenOpen(e:Event):void
		{
			_starView.visible = false;
			//dispatchEvent(new Event(Event.OPEN));
			//_waitEffect.visible = true;
			//_waitEffect.play();
		}
		
		private function onSendFenComplete(e:Event):void
		{
			//trace("ping fen resutl:" + e.target.data);
			//_waitEffect.visible = false;
			//_waitEffect.stop();
			dispatchEvent(new Event(PING_FEN_FINISH_EVENT));
		}
	
	}

}